
function mergeTableCells(tableSelector) {

    var rowflag = (window.screen.width > 2000 || window.screen.width < 1200) ? true : false;
    // 合并行（垂直方向），跳过最后一列
    $(tableSelector).find('tr').each(function () {
        const $cells = $(this).children('td, th').not(':last-child'); // 排除最后一列
        $cells.each(function (index) {
            const cell = $(this);
            if (cell.is("[rowspan]") || cell.is(":hidden") || rowflag) return;

            let rowspan = 1;
            let nextRow = $(this).closest('tr').next();

            while (nextRow.length) {
                const nextCell = nextRow.find(`td:eq(${index}), th:eq(${index})`).first();
                if (nextCell.text() === cell.text() && !nextCell.is("[rowspan]")) {
                    rowspan++;
                    nextCell.hide();
                    nextRow = nextRow.next();
                } else {
                    break;
                }
            }
            if (rowspan > 1) cell.attr('rowspan', rowspan);
        });
    });

    // 合并列（水平方向），跳过最后一列
    $(tableSelector).find('tr').each(function () {
        const $cells = $(this).children('td:visible, th:visible').not(':last-child');
        let skip = 0;

        $cells.each(function (index) {
            if (skip-- > 0 || rowflag) return;
            const currentCell = $(this);
            let colspan = 1;

            for (let nextIndex = index + 1; nextIndex < $cells.length; nextIndex++) {
                const nextCell = $cells.eq(nextIndex);
                if (nextCell.text() === currentCell.text() && !nextCell.is("[rowspan]")) {
                    colspan++;
                } else {
                    break;
                }
            }

            if (colspan > 1) {
                currentCell.attr('colspan', colspan);
                for (let i = 1; i < colspan; i++) {
                    $cells.eq(index + i).hide();
                }
                skip = colspan - 1;
            }
        });
    });
}

